//
//  LogDetailTableCell.m
//  FreeTrip
//
//  Created by 邓芮 on 15/11/24.
//  Copyright © 2015年 邓芮. All rights reserved.
//

#import "LogDetailTableCell.h"
#define contenHei 15//字体高度
#define textCor [UIColor grayColor]
@interface LogDetailTableCell()
@property (nonatomic, strong) UIImageView *headicon;//发布者头像;
@property (nonatomic, strong) UILabel *nickname;//发布者名字;
@property (nonatomic, strong) UILabel *createdate;// 创建时间;
@property (nonatomic, strong) UILabel *content;//详情;
@property (nonatomic, strong) UIImageView *image;//展示图品
@property (nonatomic, strong) UIView *sepView;//分割view;
@property (nonatomic, strong) UILabel *floor;//楼层
@end
@implementation LogDetailTableCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        
       // self.contentView.backgroundColor = [UIColor grayColor];
        self.selectionStyle = NO;
        self.headicon = [[UIImageView alloc] init];
        
        [self.contentView addSubview:_headicon];
        
        self.floor = [[UILabel alloc] init];
        self.floor.textColor = textCor;
        self.floor.font = [UIFont systemFontOfSize:KScreenHeight *13 / 568];
        [self.contentView addSubview:_floor];
        
        
        
        self.nickname = [[UILabel alloc] init];
        self.nickname.font = [UIFont systemFontOfSize:KScreenHeight *14 / 568];
        self.nickname.textColor = textCor;
        [self.contentView addSubview:_nickname];
        
        self.createdate = [[UILabel alloc] init];
        self.createdate.font = [UIFont systemFontOfSize:KScreenHeight *13 / 568];
        self.createdate.textColor = textCor;
        [self.contentView addSubview:_createdate];
        
        self.content = [[UILabel alloc] init];
        self.content.font = [UIFont fontWithName:@"HelveticaNeue" size:KScreenHeight * contenHei / 568];
        self.content.numberOfLines = 0;
        self.content.textColor = textCor;
        [self.contentView addSubview:_content];
        
        self.image = [[UIImageView alloc] init];
        [self.contentView addSubview:_image];
        
        self.sepView = [[UIView alloc] init];
        self.sepView.backgroundColor = [UIColor grayColor];
        self.sepView.alpha = 0.4;
        [self.contentView addSubview:_sepView];
    }
    return self;
}
// 赋值内容
- (void)setModel:(LogDetailModel *)model
{
    _model = model;
    [self.headicon sd_setImageWithURL:[NSURL URLWithString: _model.headicon] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    
    self.nickname.text = _model.nickname;
    self.createdate.text = _model.createdate;
    self.content.text = _model.content;
    [self.image sd_setImageWithURL:[NSURL URLWithString:_model.image] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    NSInteger num = [_model.floor integerValue];
    num = num - 1;
    self.floor.text = [NSString stringWithFormat:@"%ld楼",(long)num];
    //self.floor.text = @"1111";
    [self heightForModel];
    
}

// 设置frame
- (void)heightForModel
{
    CGFloat x = 10;
    CGFloat y = 15;
    CGFloat width = KScreenWidth * 35 / 320;
    CGFloat height = width;
    self.headicon.frame = CGRectMake(x, y, width, height);
    [self.headicon.layer setCornerRadius:width/2];
    self.headicon.layer.masksToBounds = YES;
    
    x = x + self.headicon.frame.size.width * 1.2;
    y = y * 0.8;
    width = width * 6;
    height = height / 2;
    self.nickname.frame = CGRectMake(x, y, width, height);
    y = y + self.nickname.frame.size.height + 5;
    self.createdate.frame = CGRectMake(x, y, width, height);
    CGFloat floorWidth =KScreenWidth * 40 / 320;
    self.floor.frame = CGRectMake(KScreenWidth - floorWidth, y, floorWidth, height);
   
    
    
    CGRect bounds = [_model.content boundingRectWithSize:CGSizeMake(KScreenWidth - self.headicon.frame.origin.x * 2, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:KScreenHeight * contenHei / 568]} context:nil];
    self.content.frame = CGRectMake(self.headicon.frame.origin.x, self.createdate.frame.size.height + y + 10, KScreenWidth - self.headicon.frame.origin.x * 2, bounds.size.height);
    
    y = self.content.frame.origin.y + self.content.frame.size.height + 5;
    
    if (_model.image)
    {
        height = KScreenWidth / _model.width * _model.height;
        self.image.frame = CGRectMake(0, y, KScreenWidth, height);
        y = y + self.image.frame.size.height +6;
        self.sepView.frame =CGRectMake(0, y, KScreenWidth, KScreenHeight * 8 / 568);
    }else
    {
        self.sepView.frame =CGRectMake(0, y, KScreenWidth, KScreenHeight * 8 / 568);
    }
    
    
}

// 返回高度
+ (CGFloat)heightForRowWithModel:(LogDetailModel *)model
{
    CGRect bounds = [model.content boundingRectWithSize:CGSizeMake(KScreenWidth - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:KScreenHeight * contenHei / 568]} context:nil];
    if (model.image)
    {
         CGFloat height = bounds.size.height + KScreenWidth / model.width * model.height + 10 +KScreenHeight * 25 / 568 * 1.5 + 25 ;
        return height;
    }
    else
    {
        CGFloat height = bounds.size.height + KScreenHeight * 15 / 568 * 2 + 30;
        return height;
    }
   
    
}

@end
